<?php

class AuthItemController extends AuthController
{
    public $layout='//layouts/column2';
    public $defaultAction = 'list';
    
    public function actionList($type=null)
    {
        $this->render('list', array(
            'dataProvider'=>new AuthItemDataProvider($auth = Yii::app()->authManager, $type),
        ));
    }
    
	public function actionCreate($type)
	{
        if( ! empty($_POST['AuthItemForm']))
        {
            $auth = Yii::app()->authManager;
            $model = new AuthItemForm;
            $model->type = (int)$type;
            $model->setAttributes($_POST['AuthItemForm']);
            if($model->validate())
            {
                $auth->createAuthItem($model->name, 
                    (int)$model->type, 
                    $model->description, 
                    $model->bizRule, 
                    $model->data
                );
                
                $auth->save();
                $this->redirect(array('authItem/list'));
            }    
        }
        
        $this->render('create', array(
            'type'=>$type,
        ));
	}
    
	public function actionUpdate($itemName)
	{
        $auth = Yii::app()->authManager;
        $authItem = $auth->getAuthItem($itemName);
        $authItem->attachBehavior('authType', new AuthItemBehavior);
        if( ! empty($_POST['AuthItemForm']))
        {
            $model = new AuthItemForm;
            $model->type = $authItem->getType();
            $model->setAttributes($_POST['AuthItemForm']);
            if($model->validate())
            {
                $authItem->name = $model->name;
                $authItem->description = $model->description;
                $auth->save();
                $this->redirect(array('list'));
            }    
        }
        
        $parentAuthItems = array();
        foreach($auth->getAuthItems() as $ai)
        {
            if($ai->hasChild($authItem->getName()))
            {
                $ai->attachBehavior('authType', new AuthItemBehavior);
                $parentAuthItems[$ai->getName()] = $ai;
            }
        }    
        
        $childrenAuthItems = $auth->getItemChildren($authItem->getName());
        foreach($childrenAuthItems as $childrenAuthItem)
            $childrenAuthItem->attachBehavior('authType', new AuthItemBehavior);
        
        $type = $authItem->getType();
        $childrenAuthItemSelectOptions = array('Please select...');
        if(CAuthItem::TYPE_ROLE == $type)
        {
            $roles = array_diff_key($auth->getRoles(), $childrenAuthItems, array($itemName=>$itemName));
            foreach($roles as $m)
            {
                if( ! $m->hasChild($authItem->getName()))
                    $childrenAuthItemSelectOptions['roles'][$m->getName()] = $m->getName();
            }
            
            $tasks = array_diff_key($auth->getTasks(), $childrenAuthItems, array($itemName=>$itemName));
            foreach($tasks as $m)
                $childrenAuthItemSelectOptions['tasks'][$m->getName()] = $m->getName();
            
            $operations = array_diff_key($auth->getOperations(), $childrenAuthItems, array($itemName=>$itemName));
            foreach($operations as $m)
                $childrenAuthItemSelectOptions['operations'][$m->getName()] = $m->getName();
        }
        else if(CAuthItem::TYPE_TASK == $type)
        {
            $operations = array_diff_key($auth->getOperations(), $childrenAuthItems, array($itemName=>$itemName));
            foreach($operations as $m)
                $childrenAuthItemSelectOptions['operations'][$m->getName()] = $m->getName();
        }   
        
        $this->render('update', array(
            'authItem'=>$authItem,
            'auth'=>$auth,
            'childrenAuthItemSelectOptions'=>$childrenAuthItemSelectOptions,
            'childrenAuthItems'=>$childrenAuthItems,
            'parentAuthItems'=>$parentAuthItems,
        ));
	}
    
	public function actionDelete($itemName)
	{
        $auth = Yii::app()->authManager;
        $auth->removeAuthItem($itemName);
        $auth->save();
        $this->redirect(Yii::app()->request->getUrlReferrer());
	}
    
    public function actionRemoveItemChild($itemName, $childItemName)
    {
        $auth = Yii::app()->authManager;
        $auth->removeItemChild($itemName, $childItemName);
        $auth->save();
        $this->redirect(Yii::app()->request->getUrlReferrer());
    }
    
    public function actionAddChild($itemName)
    {
        if( ! empty($_POST['childItemName']))
        {
            $childItemName = $_POST['childItemName'];
            $auth = Yii::app()->authManager;
            $auth->addItemChild($itemName, $childItemName);
            $auth->save();
            $this->redirect(Yii::app()->request->getUrlReferrer());
        }    
    }
    
    public function actionGenerate()
    {
        $generator = $this->module->getComponent('generator');
        $generator->run();
        echo 'Finished, please go back';
    }
}